<?php
namespace app\seller\controller;

class Store extends \app\common\controller\Seller
{

    /**
     * 品牌状态
     *
     * @return html
     */
    protected $_brand_state = [
        '申请中',
        '已通过'
    ];

    /**
     * 店铺设置
     *
     * @return html
     */
    public function setting()
    {
        if (request()->isPost()) {
            request()->filter([
                'htmlspecialchars'
            ]); // 设置全局过滤规则
            $data = request()->post();
            if ($this->request->file()) {
                $file = $this->uploadFile('label', 'image');
                if (! $file['status']) {
                    showDialog($file['error']);
                }
                $data['label'] = $file['save_path_type'] . $file['save_name'];//店铺logo
                $data['avatar'] = $file['save_path_type'] . $file['save_name'];//店铺头像
            }
            $result = rpc('seller/store/store_edit', [
                [
                    'id' => $this->store_id
                ],
                $data
            ]);
            if ($result['result'] >= 0) {
                showDialog('保存成功', 'reload', 'succ');
            } elseif ($result['result'] < 0) {
                showDialog($result['msg']);
            } else {
                showDialog('保存失败');
            }
        } else {
            $store = rpc('common/common/one', [
                'store',
                [
                    'id' => $this->store_id
                ]
            ]);
            $store_grade = \Fields::getById('store_grade', $store['grade_id']);
            $store['grade_name'] = $store_grade['name'];
            $this->view->assign('store', $store);
            $this->assign('left_nav', 'setting');
            return $this->view->fetch();
        }
    }

    /**
     * 商家介绍
     */
    public function intro()
    {
        if (request()->isPost()) {
            $content = request()->post('g_content/s');
            // 更新商家简介
            $data['intro'] = $content;
            $result = rpc('seller/store/store_edit', [
                [
                    'id' => $this->store_id
                ],
                $data
            ]);
            if ($result['result'] >= 0) {
                showDialog('保存成功', 'reload', 'succ');
            } elseif ($result['result'] < 0) {
                showDialog($result['msg']);
            } else {
                showDialog('保存失败');
            }
        } else {
            $store = rpc('common/common/one', [
                'store',
                [
                    'id' => $this->store_id
                ]
            ]);
            $this->assign('left_nav', 'setting');
            return view('', [
                'store' => $store
            ]);
        }
    }

    /**
     * 幻灯片设置
     */
    public function slide()
    {
        if (request()->isPost()) {
            // 更新幻灯片信息
            $update = array();
            $image_path = request()->post('image_path/a');
            $image_url = request()->post('image_url/a');
            $update['slide'] = implode(',', $image_path);
            $update['slide_url'] = implode(',', $image_url);
            $result = rpc('seller/store/store_slide_edit', [
                [
                    'id' => $this->store_id
                ],
                $update
            ]);
            if ($result['result'] >= 0) {
                showDialog('保存成功', 'reload', 'succ');
            } elseif ($result['result'] < 0) {
                showDialog($result['msg']);
            } else {
                showDialog('保存失败');
            }
        }
        
        // 删除upload中的无用数据
        $slide_list = rpc('common/common/lists', [
            'upload',
            [
                'item_id' => $this->store_id,
                'type' => 3
            ]
        ]);
        if (is_array($slide_list) && ! empty($slide_list)) {
            foreach ($slide_list as $val) {
                @unlink(ROOT_PATH . DS . 'uploads' . DS . $val['name']);
            }
        }
        $result = rpc('seller/upload/delete', [
            [
                'item_id' => $this->store_id,
                'type' => 3
            ]
        ]);
        $store_info = rpc('common/common/one', [
            'store',
            [
                'id' => $this->store_id
            ]
        ]);
        if ($store_info['slide'] != '' && $store_info['slide'] != ',,,,') {
            $this->view->assign('slide', explode(',', $store_info['slide']));
            $this->view->assign('slide_url', explode(',', $store_info['slide_url']));
        }
        //店铺默认轮播图
        $default_slide = \Fields::getById('store_open');
        $this->view->assign('default_slide', $default_slide);
        $this->assign('left_nav', 'setting');
        return $this->view->fetch();
    }

    /**
     * 店铺幻灯片ajax上传
     */
    public function silde_image_upload()
    {
        $data = request()->post();
        $file = $this->uploadFile('file', 'banner');
        if (! $file['status']) {
            showDialog($file['error']);
        }
        $img_path = $file['save_path_type'] . $file['save_name'];
        $file_id = request()->param('file_id/d');
        if ($file_id > 0) {
            // 替换upload临时表的文件信息，删除原有图片
            $file_info = rpc('common/common/one', [
                [
                    'id' => $file_id
                ]
            ]);
            @unlink(ROOT_PATH . DS . 'uploads' . DS . $file_info['name']);
            
            $update_array = array();
            $update_array['name'] = $img_path;
            $update_array['size'] = $file['size'];
            $update_array['upload_time'] = time();
            $result = rpc('seller/upload/update', [
                [
                    'id' => $file_id
                ],
                $update_array
            ]);
            $output = [];
            if ($result['result'] > 0) {
                $output['file_id'] = $file_id;
                $output['id'] = request()->param('id/s');
                $output['file_name'] = $img_path;
            } else {
                $output['error'] = $result['msg'];
            }
            echo json_encode($output);
            die();
        } else {
            // 新增upload表的文件信息
            $insert_array = array();
            $insert_array['name'] = $img_path;
            $insert_array['type'] = '3';
            $insert_array['size'] = $file['size'];
            $insert_array['item_id'] = $this->store_id;
            $insert_array['upload_time'] = time();
            $result = rpc('seller/upload/insert', [
                $insert_array
            ]);
            
            $output = [];
            if ($result['result'] > 0) {
                $output['file_id'] = intval($_POST['file_id']);
                $output['id'] = $_POST['id'];
                $output['file_name'] = $img_path;
            } else {
                $output['error'] = $result['msg'];
            }
            echo json_encode($output);
            die();
        }
    }

    /**
     * ajax删除幻灯片图片
     */
    public function slide_img_del()
    {
        $id = request()->param('file_id/d');
        // 删除upload中的无用数据
        $slide = rpc('common/common/one', [
            'upload',
            [
                'id' => $id,
                'item_id' => $this->store_id,
                'type' => 3
            ]
        ]);
        $result = $result = rpc('seller/upload/delete', [
            [
                'id' => $id,
                'item_id' => $this->store_id,
                'type' => 3
            ]
        ]);
        if ($result['result'] >= 0) {
            if (! empty($slide)) {
                @unlink(ROOT_PATH . DS . 'uploads' . DS . $slide['name']);
            }
            echo json_encode(array(
                'succeed' => '已删除'
            ));
        } elseif ($result['result'] < 0) {
            echo json_encode(array(
                'failed' => $result['msg']
            ));
        } else {
            echo json_encode(array(
                'failed' => '删除失败'
            ));
        }
        die();
    }

    /**
     * 店铺分类编辑
     */
    public function store_goods_class()
    {
        $type = request()->get('type/s');
        if ($type == 'ok') {
            $class_id = request()->get('class_id/d');
            $top_class_id = request()->get('top_class_id/d');
            $goods_class = rpc('common/common/lists', [
                'store_goods_class',
                [
                    'store_id' => $this->store_id,
                    'parent_id' => 0
                ]
            ]);
            $this->view->assign('goods_class', $goods_class);
            if ($class_id != 0) {
                // 编辑店铺分类
                $class_info = rpc('common/common/one', [
                    'store_goods_class',
                    [
                        'id' => $class_id
                    ]
                ]);
                $this->view->assign('class_id', $class_id);
                $this->view->assign('class_info', $class_info);
                return $this->view->fetch('store_goods_class_edit');
            } elseif ($top_class_id != 0) {
                // 新增二级分类
                $this->view->assign('parent_id', $top_class_id);
                return $this->view->fetch('store_goods_class_add');
            } else {
                // 新增一级分类
                return $this->view->fetch('store_goods_class_add');
            }
        } else {
            // 店铺分类列表
            $class_info = rpc('seller/store/tree_class_list', [
                [
                    'store_id' => $this->store_id
                ],
                2
            ]);
            $this->view->assign('class_info', $class_info);
            $str = '';
            if (is_array($class_info) and count($class_info) > 0) {
                foreach ($class_info as $key => $val) {
                    $row[$val['id']] = $key + 1;
                    $str .= intval(isset($row[$val['parent_id']]) ? $row[$val['parent_id']] : NULL) . ",";
                }
                $str = substr($str, 0, - 1);
            } else {
                $str = '0';
            }
            $this->view->assign('map', $str);
            $this->view->assign('class_num', count($class_info) - 1);
            $this->assign('left_nav', 'store_goods_class');
            return $this->view->fetch('store_goods_class');
        }
    }

    /**
     * 店铺分类保存
     */
    public function goods_class_save()
    {
        $id = request()->post('id/d');
        if ($id > 0) {
            // 编辑
            if (request()->isPost()) {
                request()->filter([
                    'htmlspecialchars'
                ]); // 设置全局过滤规则
                $data = request()->post();
                // 保存店铺商品分类
                if ($data) {
                    $result = rpc('seller/store/store_goods_class_edit', [
                        [
                            'id' => $id
                        ],
                        $data
                    ]);
                    if ($result['result'] >= 0) {
                        showDialog('保存成功', 'reload', 'succ');
                    } elseif ($result['result'] < 0) {
                        showDialog($result['msg']);
                    } else {
                        showDialog('保存失败');
                    }
                } else {
                    showDialog('请填写商家简介', 'reload', 'error');
                }
            } else {
                showDialog('数据错误', 'reload', 'error');
            }
        } else {
            // 新增
            if (request()->isPost()) {
                request()->filter([
                    'htmlspecialchars'
                ]); // 设置全局过滤规则
                $data = request()->post();
                if (isset($data['parent_id']) && empty($data['parent_id'])) {
                    $data['parent_id'] = 0;
                }
                $data['store_id'] = $this->store_id;
                // 保存店铺商品分类
                if ($data) {
                    $data['state'] = 1;
                    $result = rpc('seller/store/store_goods_class_add', [
                        $data
                    ]);
                    if ($result['result'] > 0) {
                        showDialog('保存成功', 'reload', 'succ');
                    } elseif ($result['result'] < 0) {
                        showDialog($result['msg']);
                    } else {
                        showDialog('保存失败');
                    }
                } else {
                    showDialog('请填写商家简介', 'reload', 'error');
                }
            } else {
                showDialog('数据错误', 'reload', 'error');
            }
        }
    }

    /**
     * 店铺分类保存
     */
    public function goods_class_del()
    {
        $ids = request()->get('class_id/s');
        $ids = explode(',', $ids);
        foreach ($ids as $key => $val) {
            if (! is_numeric($val))
                unset($ids[$key]);
        }
        if (count($ids) > 0) {
            if (request()->isGet()) {
                // 删除店铺商品分类
                $result = rpc('seller/store/store_goods_class_del', [
                    $ids
                ]);
                if ($result['result'] >= 0) {
                    showDialog('保存成功', 'reload', 'succ');
                } elseif ($result['result'] < 0) {
                    showDialog($result['msg']);
                } else {
                    showDialog('保存失败');
                }
            } else {
                showDialog('数据错误', 'reload', 'error');
            }
        } else {
            showDialog('数据错误', 'reload', 'error');
        }
    }

    /**
     * 品牌申请
     */
    public function brand()
    {
        $data = [
            'store_id' => $this->store_id,
            'status' => 1,
            'isdelete' => 0
        ];
        $name = request()->param('name/s');
        if (! empty($name)) {
            $data['name'] = $name;
        }
        $brand_list = rpc('common/common/lists', [
            'goods_brand',
            $data
        ]);
        $this->view->assign('brand_state', $this->_brand_state);
        $this->view->assign('brand_list', $brand_list);
        $this->assign('left_nav', 'brand');
        return view('', [
            'brand_list' => $brand_list
        ]);
    }

    /**
     * 品牌申请编辑
     */
    public function brand_edit()
    {
        $brand_id = request()->get('id/d');
        if (! $brand_id) {
            showDialog('品牌不存在', 'reload', 'error');
        }
        $brand_info = rpc('common/common/one', [
            'goods_brand',
            [
                'id' => $brand_id,
                'store_id' => $this->store_id
            ]
        ]);
        if (empty($brand_info)) {
            showDialog('品牌不存在', 'reload', 'error');
        }
        $this->view->assign('brand_info', $brand_info);
        $goods_class = config('mall_cate');
        $this->view->assign('goods_class', $goods_class);
        $this->assign('left_nav', 'brand');
        return view();
    }

    /**
     * 品牌申请添加
     */
    public function brand_add()
    {
        $goods_class = config('mall_cate');
        $this->view->assign('goods_class', $goods_class);
        $this->assign('left_nav', 'brand');
        return $this->view->fetch();
    }

    /**
     * 品牌申请保存
     */
    public function brand_save()
    {
        $id = request()->post('id/d');
        if ($id > 0) {
            // 编辑
            if (request()->isPost()) {
                request()->filter([
                    'htmlspecialchars'
                ]); // 设置全局过滤规则
                $data = request()->post();
                $file = $this->uploadFile('pic', 'brand');
                if ($file['status']) {
                    // 编辑的时候，图片上传正常，再替换图片
                    $data['pic'] = $file['save_path_type'] . $file['save_name'];
                }
                // 保存店铺商品分类
                if ($data) {
                    $result = rpc('seller/store/brand_save', [
                        'edit',
                        [
                            'id' => $id
                        ],
                        $data
                    ]);
                    if ($result['result'] >= 0) {
                        showDialog('保存成功', 'reload', 'succ');
                    } elseif ($result['result'] < 0) {
                        showDialog($result['msg']);
                    } else {
                        showDialog('保存失败');
                    }
                } else {
                    showDialog('数据错误', 'reload', 'error');
                }
            } else {
                showDialog('数据错误', 'reload', 'error');
            }
        } else {
            // 新增
            if (request()->isPost()) {
                request()->filter([
                    'htmlspecialchars'
                ]); // 设置全局过滤规则
                $data = request()->post();
                $file = $this->uploadFile('pic', 'brand');
                if (! $file['status']) {
                    showDialog($file['error']);
                }
                $data['pic'] = $file['save_path_type'] . $file['save_name'];
                $data['store_id'] = $this->store_id;
                $data['apply'] = 0;
                // 保存品牌
                if ($data) {
                    $result = rpc('seller/store/brand_save', [
                        'add',
                        '',
                        $data
                    ]);
                    if ($result['result'] >= 0) {
                        showDialog('保存成功', 'reload', 'succ');
                    } elseif ($result['result'] < 0) {
                        showDialog($result['msg']);
                    } else {
                        showDialog('保存失败');
                    }
                } else {
                    showDialog('请填写商家简介', 'reload', 'error');
                }
            } else {
                showDialog('数据错误', 'reload', 'error');
            }
        }
    }

    /**
     * 品牌申请提交
     */
    public function brand_del()
    {
        $id = request()->get('id/d');
        if ($id > 0) {
            // 删除
            if (request()->isGet()) {
                // 删除店铺商品分类
                $data['isdelete'] = 1; // 假删除
                $result = rpc('seller/store/brand_save', [
                    'delete',
                    [
                        'id' => $id
                    ],
                    $data
                ]);
                if ($result['result'] >= 0) {
                    showDialog('删除成功', 'reload', 'succ');
                } else {
                    showDialog('删除失败');
                }
            } else {
                showDialog('数据错误', 'reload', 'error');
            }
        } else {
            // 新增
            showDialog('数据错误', 'reload', 'error');
        }
    }
}
